/* 
 * File:   scanner.cpp
 * Author: ScaV
 *
 * Created on Oct 29, 2011, 4:53:46 PM
 */

#include <cstdlib>
#include <iostream>
#include <cstring>
#include <vector>
#include "scanner.h"
#include "tables.h"
/*
 * Simple C++ Test Suite
 */

void test1() {
	std::cout << "scanner test 1" << std::endl;
	try {
		scanner a("thereisnofile.txt");
	} catch (const char *str) {
		if (!strcmp(str, "Can't open scanner input file!"))
			return;
	}
	std::cout << "%TEST_FAILED% time=0 testname=throw (constructor openfile) message=There is no throw in constructor!" << std::endl;
}

void test2() {
	std::cout << "scanner test 2" << std::endl;
	scanner a("in.txt");
	lexeme *lx;
	char *str;
	lx = a.getNextLex();
	str = lx->getName();
	delete lx;
	if (!strcmp(str, "double") && lx->type == LEX_KEYWORD) {
		delete[] str;
		return;
	}
	delete[] str;
	std::cout << "%TEST_FAILED% time=0 testname=keyword (Get First Lexeme & type) message=Invalid lexeme or LEX_TYPE!" << std::endl;
}

void  test3(scanner& scn, std::vector <lexeme*>& result) {
	lexeme *lx;
	char *str;
try {
	for (int i = 0; ; ++i) {
		lx = scn.getNextLex();
		if (i == 17) {
			str = lx->getName();
			if (strcmp(str, "23453.345") || lx->type != LEX_CONSTANT || lx->line != 3)
				throw "Invalid answer!";
		}
		if (i == 69) {
			str = lx->getName();
			if (strcmp(str, "555.12d12") || lx->type != LEX_UNKNOWN || lx->line != 9)
				throw "Invalid answer!";
		}
		if (!lx)
			break;
		result.push_back(lx);
	}
	delete[] str;
	delete lx;
	return;
} catch (const char *str) {
	delete[] str;
	delete lx;
	std::cout << "%TEST_FAILED% time=0 testname=scanner (complex) message=" << str << std::endl;
}
}

int main(int argc, char** argv) {
	std::cout << "%SUITE_STARTING% scanner" << std::endl;
	std::cout << "%SUITE_STARTED%" << std::endl;

	std::cout << "%TEST_STARTED% testname=throw (constructor openfile)" << std::endl;
	test1();
	std::cout << "%TEST_FINISHED% time=0 throw (constructor openfile)" << std::endl;

	std::cout << "%TEST_STARTED% keyword (Get First Lexeme & type)\n" << std::endl;
	test2();
	std::cout << "%TEST_FINISHED% time=0 keyword (Get First Lexeme & type)" << std::endl;
	
	std::cout << "%TEST_STARTED% scanner (complex)\n" << std::endl;
	scanner *scn;
	std::vector <lexeme*> *result;
	scn = new scanner("in.txt");
	result = new std::vector <lexeme*>;
	test3(*scn, *result);
	delete scn;
	delete result;
	std::cout << "%TEST_FINISHED% time=0 scanner (complex)" << std::endl;
	std::cout << "%SUITE_FINISHED% time=0" << std::endl;
	return (EXIT_SUCCESS);
}

